<template>
  <div class="common-layout">
    <el-container>
      <el-header>Header</el-header>
      <el-container>
        <el-aside width="200px">Aside</el-aside>
        <el-main>
          <el-button type="primary" style="position: absolute; right: 0px; margin: 10px" @click="addTodo"
            >新增todo</el-button
          >
          <el-table :data="tableData" border style="width: 100%; margin-top: 52px">
            <el-table-column prop="id" label="id" />
            <el-table-column prop="title" label="title" />
            <el-table-column prop="completed" label="completed">
              <template #default>
                <el-button link type="primary" size="small" @click="editTodo"> 编辑todo </el-button>
                <el-button link type="primary" size="small" @click="deleteTodo">删除todo</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-main>
      </el-container>
    </el-container>
  </div>
  <el-dialog v-model="centerDialogVisible" title="增加todo" width="500" align-center>
    <el-form :model="form" label-width="auto" style="max-width: 600px">
      <el-form-item label="id">
        <el-input v-model="form.id" />
      </el-form-item>
      <el-form-item label="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="completed">
        <el-switch v-model="form.completed" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addTodoConfirm"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { TodosApi } from '@/api/modules/todos'
import { onMounted } from 'vue'
import { TodosApiNamespace } from '@/api/interface/todos'
const centerDialogVisible = ref(false)
const form = reactive({
  id: '',
  title: '',
  completed: true
})
const addTodo = () => {
  console.log('click')
  centerDialogVisible.value = true
}
const addTodoConfirm = async () => {
  const res = await TodosApi.crateTodos(form)
  console.log(res, '添加todo的返回值')
  if (res.data) {
    centerDialogVisible.value = false
    getAllTodosFun()
  }
}
const editTodo = () => {
  console.log('click')
}
const deleteTodo = () => {
  console.log('click')
}
const tableData = ref<Array<TodosApiNamespace.getAllTodosRes>>([])
const getAllTodosFun = async () => {
  const res = await TodosApi.getAllTodos()
  console.log(res, '获取全部todo ,接口返回值')
  tableData.value = res.data
}
onMounted(() => {
  getAllTodosFun()
})
</script>
<style scoped>
:deep(.el-header) {
  background-color: pink;
}
:deep(.el-aside) {
  background-color: pink;
  height: calc(100vh - 60px);
}
</style>
